package session.stateless;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.ejb.Stateless;
import java.util.ArrayList;
import entity.AccessRole;
import entity.AccessControl;
import javax.ejb.EJB;
import entity.Employee;
import java.util.Collection;

@Stateless
public class AccessRoleSessionBean implements AccessRoleSessionBeanLocal {
    
    @PersistenceContext
    private EntityManager entityManager;
    
    @EJB
    EmployeeSessionBeanLocal employeeSessionBeanLocal;
    
    @EJB
    AccessControlSessionBeanLocal accessControlSessionBeanLocal;
    
     @Override
    public ArrayList <AccessRole> getAccessRoles ()
    {
        Query query = entityManager.createQuery("SELECT u FROM AccessRole u");
        ArrayList<AccessRole> roles = new ArrayList<AccessRole>();
        for (Object o : query.getResultList()) {
            AccessRole r = (AccessRole) o;
            roles.add(r);
        }
        return roles;
    }
     @Override
     public ArrayList <AccessRole> getAccessRoles (String title)
     {
         Query query = entityManager.createQuery("SELECT u FROM AccessRole u");
        ArrayList<AccessRole> roles = new ArrayList<AccessRole>();
        for (Object o : query.getResultList()) {
            AccessRole r = (AccessRole) o;
            Boolean found = false;
            for (AccessControl ac: r.getAccessControls())
            {
                if (ac.getTitle().compareTo(title)==0)
                {
                    found = true;
                }
            }
            if(found)
            roles.add(r);
        }
        return roles;
     }
     
     
     @Override
    public void addRole (String roleType, String description)
    {
        AccessRole accessRole = entityManager.find(AccessRole.class, roleType);
        if (accessRole == null)
        {
            accessRole = new AccessRole (roleType, description);
            entityManager.persist(accessRole);
            entityManager.flush();
        }
    }
     @Override
    public AccessRole getRole (String roleType)
    {
        AccessRole accessRole = entityManager.find(AccessRole.class, roleType);
        return accessRole;
    }
     @Override
    public ArrayList <AccessRole> getRoles ()
    {
        Query query = entityManager.createQuery("SELECT u FROM AccessRole u");
        ArrayList<AccessRole> roles = new ArrayList<AccessRole>();
        for (Object o : query.getResultList()) {
            AccessRole a = (AccessRole) o;
            roles.add(a);
        }
        return roles;
    }
    
    @Override
    public void updateRole (String roleType, String description)
    {
        AccessRole accessRole = entityManager.find(AccessRole.class, roleType);
 
        if (accessRole != null)
        {
               accessRole.setDescription(description);
               entityManager.persist(accessRole);
            entityManager.flush();
        
        }
    }
    
    @Override
    public void deleteRole (String roleType)
    {
         AccessRole accessRole = entityManager.find(AccessRole.class, roleType);
         if (accessRole != null)
         {
                accessRole.getAccessControls().clear();
              
                
                for (Employee e: employeeSessionBeanLocal.getEmployees())
                {
                    if (e.getAccessRoles().contains(accessRole))
                    {
                        e.getAccessRoles().remove(accessRole);        
                            entityManager.persist(e);
                            entityManager.flush();
                        }
                    }
                }
                
                entityManager.remove(accessRole);
         }
    

    @Override
    public ArrayList<String> getControls(String roleType) {

        
        AccessRole accessRole = entityManager.find(AccessRole.class, roleType);
        ArrayList<String> a = new ArrayList<String>();
        for (AccessControl o : accessRole.getAccessControls()) {
            a.add(o.getTitle());
        }
        return a;
    }
    
    @Override
    public void updateAccessControl(String roleType, String title, Boolean addRole) {
        AccessRole accessRole = entityManager.find (AccessRole.class, roleType);
        Collection<AccessControl> controls = accessRole.getAccessControls();

        AccessControl a = accessControlSessionBeanLocal.getControl(title);
        if (addRole) {
            controls.add(a);
        } else {
            controls.remove(a);
        }

        entityManager.persist(accessRole);
        entityManager.flush();
    }
    
}
    
